const ci = require('miniprogram-ci')
const chalk = require('chalk')

class UploadApplet {
  constructor ({
    name,
		projectPath,
		appid,
		privateKeyPath,
    desc,
    robot,
		version,
    uploadOptions,
		previewOptions,
  } = data) {

    console.log(chalk.yellow('当前小程序名称: ', name))
    this.project = new ci.Project({
      projectPath,
      type: 'miniProgram',
      appid,
      privateKeyPath,
      ignores: [
        `${projectPath}/node_modules/**/*`,
        `${projectPath}/CHANGELOG.md`,
        `${projectPath}/README.md`,
        `${projectPath}/yarn.lock`,
        `${projectPath}/package-lock.json`,
      ],
    })

    this.commonConfig = {
      version,
      robot,
      desc,
      setting: {
        es6: true,
        es7: true,
        minify: true,
        ignoreUploadUnusedFiles: true,
      },
    }

    this.uploadOptions = uploadOptions
    this.previewOptions = previewOptions
  }

  // 上传体验版
  upload () {
    console.log(chalk.yellow('开始上传小程序'))
    const appltUploadOptions = {
      project: this.project,
      ...this.commonConfig,
      ...this.uploadOptions
    }
    console.log(chalk.yellow('ci.Upload 项目参数: \n', JSON.stringify(appltUploadOptions)))
    return ci.upload(appltUploadOptions)
  }

  preview () {
    console.log(chalk.yellow('开始预览小程序'))
    const appltPreviewOptions = {
      project: this.project,
      ...this.commonConfig,
      ...this.previewOptions,
      qrcodeFormat: 'image',
			qrcodeOutputDest: './src/image/previewQrCode.jpg',
    }
    console.log(chalk.yellow('ci.preview 项目参数: \n', JSON.stringify(appltPreviewOptions)))
    return ci.preview(appltPreviewOptions)
  }
}

module.exports = { UploadApplet }